// components/pull-refresh/pull-refresh.js
const app = getApp();
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  externalClasses: ['im-pull-class', 'im-content-wrap'],
  /**
   * 组件的属性列表
   */
  properties: {
    post: {
      type: Object,
      value: {}
    },
    url: {
      type: String,
      value: ''
    },
    header: {
      type: String,
      value: 'application/x-www-form-urlencoded'
    },
    height: {
      type: Number,
      value: 0
    },
    showtoastloding: {
      type: Boolean,
      value: false
    },

    enableBackToTop: {
      type: Boolean,
      value: true
    },
    
    emptyUrl: {
      type: String,
      value: '/imgs/kong.png'
    },
    emptyText: {
      type: String,
      value: '暂无相关数据'
    },
    color: {
      type: String,
      value: '#CAAB00'
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    page: 1,
    end: false,
    emptyShow: false,
    requesting: false,
    color: "#3F82FD",
    scroll: null,
  },
  created: function () {
    this.scroll = this.selectComponent('.scroll');
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 刷新数据
    refresh() {
      this.reset();
      this.getList('refresh');
    },
    // 加载更多
    pullUp() {
      this.getList('more');
    },
    reset() {
      this.setData({
        page: 1,
      });
      this.scroll.data.downStatus = '';
    },
    startUp() {
      if (this.scroll.data.downStatus == 'noData') {
        throw new Error('没有更多数据');
      }
      this.scroll.startLoad();
    },
    stopUp() {
      this.scroll.stopLoad();
    },
    stopRefresh() {
      this.scroll.stop();
    },
    noData() {
      this.scroll.noData();
    },

    //获取数据方法
    getList(type) {
      let _this = this;
      if(type === 'more') {
        try {
          this.startUp();
        } catch (error) {
          return;
        }
      }
      //ajax请求参数
      let param = _this.data.post;
      param['page'] = _this.data.page
      app.http.$ajax({
        url: _this.data.url,
        data: param,
        loading: _this.data.showtoastloding,
        headers: {
          'Content-Type': this.data.header
        }
      }, {
          success(res) {
            //请求成功
            let data = res.data.data;
            if (type == 'more') {
              //上啦加载
              _this.triggerEvent('more', res) // 只会触发 pageEventListener2
            } else {
              //下拉加载
              _this.triggerEvent('refresh', res) //
            }
          },
          complete(res) {
            console.log(res)
            if (!app.util.isUndefined(res.code) && res.code == 1) {
              let data = [], per_page
              if (app.util.isArray(res.data)) {
                data = res.data;
                per_page = _this.data.page;
              } else if (!res.data) {
                data = [];
                per_page = _this.data.page;
              } else {
                //请求成功
                data = res.data.data;
                per_page = res.data.per_page;
              }
              //请求成功 如果 没有数据展示空
              if (app.util.isArray(data) && data.length < per_page) {
                //没有更多数据
                _this.noData();
              } 
              _this.setData({
                page: per_page + 1
              });
            }
            if (type == 'more') {
              //上啦加载
              _this.stopUp();
            } else {
              //下拉加载
              _this.stopRefresh() //
            }
          },
          fail(res) {
            if (type == 'more') {
              //上啦加载
              _this.stopUp();
            } else {
              //下拉加载
              _this.stopRefresh() //
            }
          }
        })
    }
  }
})
